package com.example.houseman.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.houseman.entity.TbHouseInfo;
import com.example.houseman.entity.TbRelationHousePicture;
import com.example.houseman.entity.TbRelationRoomPicture;
import com.example.houseman.entity.TbRoomInfo;
import com.example.houseman.service.ITbHouseInfoService;
import com.example.houseman.service.ITbRelationHousePictureService;
import com.example.houseman.service.ITbRelationRoomPictureService;
import com.example.houseman.service.ITbRoomInfoService;
import com.org.dto.HouseInfoDto;
import com.org.dto.TbHouseInfoDto;
import com.org.vo.ResultEntity;
import io.micrometer.core.annotation.Timed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 龚梦1
 * @since 2021-03-31
 */
@RestController
@RequestMapping("/hou/TbHouseInfoController")
public class TbHouseInfoController {

    @Autowired
    private ITbHouseInfoService tbHouseInfoService;

    @Autowired
    private ITbRoomInfoService iTbRoomInfoService;

    @Autowired
    private ITbRelationHousePictureService iTbRelationHousePictureService;

    @Autowired
    private ITbRelationRoomPictureService iTbRelationRoomPictureService;


    @RequestMapping("savehouseinfo")
    public ResultEntity savehouseinfo(@RequestBody TbHouseInfoDto tbHouseInfoDto) {
        //  System.err.println(tbHouseInfoDto);
        return tbHouseInfoService.saveinfo(tbHouseInfoDto);
    }


    @RequestMapping("/insert")
    @Timed
    public ResultEntity insert(@RequestBody TbHouseInfo tbHouseInfo) {

        String now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        tbHouseInfo.setCreateTime(now);
        System.out.println(tbHouseInfo);
        boolean save = tbHouseInfoService.save(tbHouseInfo);
        System.out.println(save);
        TbHouseInfo houseinfo = new TbHouseInfo();
        //TbRoomInfo表
        TbRoomInfo ti = new TbRoomInfo();
        //添加room inif表
        if (tbHouseInfo.getRental() != null) {
            ti.setHouseId(tbHouseInfo.getId());
            System.out.println(tbHouseInfo.getId());
            ti.setRental(tbHouseInfo.getRental());
            ti.setPrivateFacility(tbHouseInfo.getPrivateFacility());
            boolean save1 = iTbRoomInfoService.save(ti);
            System.out.println(save1);
        }
        //TbRelationHousePicture表
        TbRelationHousePicture rhp = new TbRelationHousePicture();
        //判断公共区域照片
        if (tbHouseInfo.getImgHone() != null && tbHouseInfo.getImgHone() != "") {
            rhp.setHouseId(tbHouseInfo.getId());
            rhp.setPictureUrl(tbHouseInfo.getImgHone());
            boolean save1 = iTbRelationHousePictureService.save(rhp);
            System.out.println(save1);
        }
        //TbRelationRoomPicture表
        TbRelationRoomPicture rrp = new TbRelationRoomPicture();
        //判断房间区域照片
        if (tbHouseInfo.getImgRoom() != null && tbHouseInfo.getImgRoom() != "") {
            rrp.setRoomId(ti.getId());
            System.out.println(ti.getId());
            rrp.setPictureUrl(tbHouseInfo.getImgRoom());
            final boolean save1 = iTbRelationRoomPictureService.save(rrp);
            System.out.println(save1);
        }

        if (save) {
            return ResultEntity.ok();
        }
        return null;
    }

    /**
     * 选择房产弹出List列表带分页
     */
    @RequestMapping("getHouse")
    public ResultEntity getHouse(@RequestBody HouseInfoDto houseInfoDto) {

        Page<TbHouseInfo> page = new Page<>();

        page.setCurrent(houseInfoDto.getPageNum());

        page.setSize(houseInfoDto.getPageSize());

        Page<TbHouseInfo> page1 = tbHouseInfoService.getHouse(houseInfoDto, page);

        System.out.println(page1.getRecords());

        return ResultEntity.ok("200", "查询成功", page1);
    }

    @RequestMapping("getExtensionHouse")
    public ResultEntity getExtensionHouse(@RequestBody HouseInfoDto houseInfoDto) {

        Page<TbHouseInfo> page = new Page<>();

        page.setCurrent(houseInfoDto.getPageNum());

        page.setSize(houseInfoDto.getPageSize());

        Page<TbHouseInfo> page1 = tbHouseInfoService.getExtensionHouse(houseInfoDto, page);

        System.out.println(page1.getRecords());

        return ResultEntity.ok("200", "查询成功", page1);
    }

}
